package cn.edu.scau.cmi.linyiqu.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;



import cn.edu.scau.cmi.linyiqu.domain.Teacher;
import cn.edu.scau.cmi.linyiqu.dao.BaseHibernateDAO;



public class TeacherDAO extends BaseHibernateDAO {
 // private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
  // property constants
  public static final String NAME = "name";

  public void save(Teacher teacher) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
      session.save(teacher);
      transaction.commit();
      session.close();   
  }

  public void delete(Teacher teacher) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
      session.delete(teacher);
      transaction.commit();
      session.close();
  }
  
  public void update(Teacher teacher) {
	  Session session = getSession();
	  Transaction transaction = session.beginTransaction();
      getSession().update(teacher);
      transaction.commit();
      session.close();
  }

  public Teacher findById(java.lang.Long id) {
	  Session session = getSession();
      Teacher teacher = (Teacher)session.get("cn.edu.scau.cmi.linyiqu.domain.Teacher" , id);
      session.close();
      return teacher;
  }

  public List<Teacher> findByName(String name) {
	Session session = getSession();
	String queryString = "from Teacher where name like '%" + name + "%' ";
	
//	List<Teacher> teachers = session.createSQLQuery(queryString).addEntity(Teacher.class).list();
	List<Teacher> teachers = session.createQuery(queryString).list();
    session.close();
    return teachers;
    
  }

  public List<Teacher> findAll() {
	  Session session = getSession();
      String queryString = "from Teacher";
//      List<Teacher> teacher = session.createSQLQuery(queryString).addEntity(Teacher.class).list();
      List<Teacher> teacher = session.createQuery(queryString).list();
      session.close();
      return teacher;

  }

  

  
}
